const { app, BrowserWindow } = require('electron');
const path = require('path');
const { session } = require("electron");
const { sockProxyRules } = require('./proxy/socksProxy/socksProxy');
// const { httpProxyRules } = require('./proxy/httpProxy/httpProxy');

let mainWindow;

async function createWindow() {
    const ses = session.defaultSession;
    const proxyRules = await sockProxyRules("[socks4/5]://[userId]:[password]@[host]:[port]");
    // const proxyRules = await httpProxyRules("[http/https]://[userId]:[password]@[host]:[port]");
    ses.setProxy({ proxyRules: proxyRules.url });

    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: false, // 禁用 Node.js 在渲染进程
            contextIsolation: true, // 安全隔离
            webviewTag: true,
        },
    });

    mainWindow.loadFile(path.join(__dirname, 'main.html'));

    mainWindow.on('closed', () => {
        mainWindow = null;
    });
}

app.commandLine.appendSwitch('ignore-certificate-errors');
app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit();
});

app.on('activate', () => {
    if (mainWindow === null) createWindow();
});